﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using System.Web.Security;

namespace FB.Membership
{
    public class FBProfile : ProfileBase
    {
        [SettingsAllowAnonymous(false)]
        public string BranchCode
        {
            get { return base["BranchCode"] as string; }
            set { base["BranchCode"] = value; }
        }


        public static FBProfile CreateFBProfile(string username)
        {
            return ProfileBase.Create(username, true) as FBProfile;
        }


        public static FBProfile Current
        {
            get { return HttpContext.Current.Profile as FBProfile; }
        }


        #region Service

        public List<string> ServiceList
        {
            get { return GetValueOrDefault(base["ServiceList"] as List<string>, new List<string>()); }
        }

        public void RemoveService(string AppKey)
        {
            var services = ServiceList;
            services.Remove(AppKey);
            base["ServiceList"] = services;
            Save();
        }

        public void AddService(string AppKey)
        {
            var services = ServiceList;
            if (!services.Contains(AppKey, StringComparer.InvariantCultureIgnoreCase))
            {
                services.Add(AppKey);
                base["ServiceList"] = services;
                Save();
            }
        }

        #endregion


        T GetValueOrDefault<T>(T Value, T DefaultValue)
        {
            return (null != Value) ? Value : DefaultValue;
        }

    }




}
